<template>
  <ContentWrap v-if="routerTitle !== '会员详情'">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.AUDITSTATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['blind:member-user:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="退款编码" align="center" prop="no" />
      <el-table-column label="活动编码" align="center" prop="activeNo" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="状态" prop="status" width="90">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AUDITSTATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row.id)"
            v-if="scope.row.status !== 0"
            >详情</el-button
          >
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['trade:refund:audit']"
            v-if="scope.row.status === 0"
          >
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <Form ref="formRef" @success="getList" />
  <!-- 详情弹窗：查看 -->
  <Detail ref="formRefDetail" />
</template>

<script setup lang="ts">
import * as TradeRefundApi from '@/api/blind/tradeRefund'
import { dateFormatter } from '@/utils/formatTime'
import Form from './Form.vue'
import Detail from './Detail.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

defineOptions({ name: 'BlindMemberUser' })

const { user } = withDefaults(defineProps<{ user?: any }>(), { user: null })
// 获取路由
const router = useRouter()
const routerTitle = router.currentRoute.value.meta.title

/** 初始化 **/
onMounted(() => {
  getList()
})

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  no: undefined,
  activityNo: undefined,
  nickname: undefined,
  userId: undefined,
  refundAmount: undefined,
  auditStatus: undefined,
  createTime: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单

const getList = async () => {
  queryParams.userId = user?.id
  loading.value = true
  try {
    const data = await TradeRefundApi.getTradeRefundPage(queryParams)

    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 审核操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 打开会员详情 */
const formRefDetail = ref()
const openDetail = (id?: number) => {
  formRefDetail.value.open(id)
}
</script>
